from datetime import datetime

from models import db


class User(db.Model):
    """用户"""
    __tablename__ = "user"

    id = db.Column(db.Integer, primary_key=True)  # 用户编号
    nick_name = db.Column(db.String(32), unique=True, nullable=False)  # 用户昵称
    password_hash = db.Column(db.String(128), nullable=False)  # 加密的密码
    avatar_url = db.Column(db.String(256))  # 用户头像路径
    city = db.Column(db.String(50))  # 城市
    fly_kiss = db.Column(db.Integer, default=0)
    email = db.Column(db.String(64), unique=True, nullable=False)  # 邮箱
    create_time = db.Column(db.DateTime, default=datetime.now)  # 注册时间
    last_login = db.Column(db.DateTime, default=datetime.now)  # 最后一次登录时间
    is_admin = db.Column(db.Boolean, default=False)  # 是否是管理员
    vip_leven = db.Column(db.Integer, default=0)  # vip等级
    signature = db.Column(db.String(512))  # 用户签名
    credentials = db.Column(db.String(50))  # 认证信息
    gender = db.Column(  # 性别
        db.Enum(
            "MAN",  # 男
            "WOMAN"  # 女
        ),
        default="MAN"
    )
    e_status = db.Column(db.Boolean, default=False)  # 是否激活 1是激活
    activekey = db.Column(db.String(50), nullable=True)  # 激活用的验证码


    def to_user_list_dict(self):
        ret = {
            "id": self.id,
            "fly_kiss": self.fly_kiss,
            "avatar_url": self.avatar_url,
            "is_admin": self.is_admin,
            "vip_leven": self.vip_leven,
            "create_time": self.create_time.strftime("%Y-%m-%d"),

        }

        return ret


class Post(db.Model):
    """贴"""
    __tablename__ = "post"

    id = db.Column(db.Integer, primary_key=True)  # 编号
    title = db.Column(db.String(256), nullable=False)  # 标题
    source = db.Column(db.String(64), nullable=False)  # 来源
    content = db.Column(db.Text, nullable=False)  # 内容
    clicks = db.Column(db.Integer, default=0)  # 浏览量
    index_image_url = db.Column(db.String(256))  # 图片路径
    fly_kiss = db.Column(db.Integer, default=0)
    top = db.Column(db.Boolean, default=False)  # 置顶
    c_status = db.Column(db.Boolean, default=False)  # 未结
    best = db.Column(db.Boolean, default=False)  # 加精
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间
    category_id = db.Column(db.Integer, db.ForeignKey("category.id"))
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))  # 当前创贴的作者id

    user = db.relationship('User', backref=db.backref('post', lazy='dynamic'))  # 用户
    category = db.relationship('Category', backref='post')

    comments = db.relationship("Comment", lazy="dynamic",backref='post')

    def to_dict(self):
        """
        定义一个方法，用来将将对象中的部分属性，转换为字典
        :return: 一个字典
        """
        ret = {
            "id": self.id,
            "create_time": self.create_time,
            "fly_kiss": self.fly_kiss,
            "index_image_url": self.index_image_url,
            "source": self.source,
            "user": self.user,
            "top": self.top,
            "c_status": self.c_status,
            "best": self.best,
            "title": self.title,
            "category": self.category,
            "comments": self.comments,
        }
        return ret

    def to_post_list_dict(self):
        ret = {
            "id": self.id,
            "fly_kiss": self.fly_kiss,
            "index_image_url": self.index_image_url,
            "source": self.source,
            "title": self.title,
            "create_time": self.create_time.strftime("%Y-%m-%d"),

        }

        return ret
class Category(db.Model):
    """贴分类"""
    __tablename__ = "category"

    id = db.Column(db.Integer, primary_key=True)  # 分类编号
    name = db.Column(db.String(64), nullable=False)  # 分类名
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间





class Comment(db.Model):
    """评论"""
    __tablename__ = "comment"
    id = db.Column(db.Integer, primary_key=True)  # 评论编号
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)  # 用户id
    post_id = db.Column(db.Integer, db.ForeignKey("post.id"), nullable=False)  # id
    content = db.Column(db.Text, nullable=False)  # 评论内容
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now)  # 记录的更新时间
    # 评论的创建者
    user = db.relationship('User', backref=db.backref('comments', lazy='dynamic'))
    # 实现对评论的回复（其实就是自关联）
    parent_id = db.Column(db.Integer, db.ForeignKey("comment.id"))  # 父评论id
    parent = db.relationship("Comment", remote_side=id)  # 自关联
    # 点赞的用户
    like_user = db.relationship("User", secondary="comment_like", lazy='dynamic', backref="like_comment")

class CommentLike(db.Model):
    """
    评论点赞
    评论表 与 用户表之间是多对多关系，因为一个评论可以被多个用户点赞，一个用户也可以点赞多个评论
    所以，此表就相当于Comment与User的 中间表
    """
    __tablename__ = "comment_like"
    comment_id = db.Column("comment_id", db.Integer, db.ForeignKey("comment.id"), primary_key=True)  # 评论编号
    user_id = db.Column("user_id", db.Integer, db.ForeignKey("user.id"), primary_key=True)  # 用户编号




class Follow(db.Model):
    """好友"""
    __tablename__ = "follow"
    followed_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)  # 被加的
    follower_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)  # 加好友的
